﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DAL = AccessDAL;
using CommonFunc;

public partial class Manageweb_ManageNews_ManageNewsClass : System.Web.UI.Page
{
    DAL.NewsClassTableAdapters.NewsClassTableAdapter DNC =
        new DAL.NewsClassTableAdapters.NewsClassTableAdapter();

    MODEL.NewsClass MNC = new MODEL.NewsClass();
    MODEL.NewsClass _MNC = new MODEL.NewsClass();

    BLL.NewsClass BNC = new BLL.NewsClass();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Cookies["MasterID"] == null)
        {
            Response.Redirect("../Default.aspx");
        }

        if (!IsPostBack)
        {
            DDL_ClassDataBind();
        }

        ClickInit();
    }


    #region ClickInit [初始化]
    protected void ClickInit()
    {
        try
        {
            LB_ManageNewsClass.Text = "";
            TABLE_Add_Mod.Visible = false;

            if (LB_ID.Text == "")
            {
                Btn_Submit.Click += new EventHandler(Btn_Add_Click);
                LB_TableTitle.Text = "添加类别";
                
            }
            else
            {
                Btn_Submit.Click += new EventHandler(Btn_Mod_Click);
                LB_TableTitle.Text = "修改类别";
                
            }
        }
        catch (Exception ex)
        {
            LB_ManageNewsClass.Text = ex.Message;
        }
    }
    #endregion


    #region 表格数据 [初始化]
    protected void TableDataInit()
    {
        try
        {
            if (LB_ID.Text == "")
            {
                TB_Title.Text = "";

                DDL_Class.SelectedValue = "";
            }
            else
            {
                DDL_ClassDataBind();

                MNC.NC_ID = Convert.ToInt32(LB_ID.Text);

                _MNC = BNC.GetDataByID(MNC);

                TB_Title.Text = _MNC.NC_Title;

                DDL_Class.SelectedValue = _MNC.NC_ParentID.ToString();
            }
        }
        catch (Exception ex)
        {
            LB_ManageNewsClass.Text = ex.Message;
        }
    }
    #endregion


    #region DDL_Class数据绑定 [初始化]
    protected void DDL_ClassDataBind()
    {
        try
        {
            DDL_Class.Items.Clear();

            ListItem LI0 = new ListItem("", "");
            DDL_Class.Items.Add(LI0);
            ListItem LI1 = new ListItem("根目录", "0");
            DDL_Class.Items.Add(LI1);

            DataTable DT = DNC.GetDataOrderByClassCode(false);

            foreach (DataRow dr in DT.Rows)
            {
                ListItem li = new ListItem(
                    FormatClassTitle(dr["NC_Title"], dr["NC_Level"]), dr["NC_ID"].ToString());
                DDL_Class.Items.Add(li);
            }
        }
        catch (Exception ex)
        {
            LB_ManageNewsClass.Text = ex.Message;
        }
    }
    #endregion


    #region 类别标题 [格式化]
    protected string FormatClassTitle(object _Title, object _Level)
    {
        return ClassCode.CreateClassListItemText(_Title.ToString(), _Level.ToString());
    }
    #endregion


    #region 添加类别 [Command] [GridView]
    protected void LBtn_Add_Command(object sender, CommandEventArgs e)
    {
        try
        {
            TABLE_Add_Mod.Visible = true;

            LB_ID.Text = "";

            TableDataInit();

            DDL_Class.Enabled = true;
        }
        catch (Exception ex)
        {
            LB_ManageNewsClass.Text = ex.Message;
        }
    }
    #endregion
    #region 修改类别 [Command] [GridView]
    protected void LBtn_Mod_Command(object sender, CommandEventArgs e)
    {
        try
        {
            TABLE_Add_Mod.Visible = true;

            LB_ID.Text = e.CommandArgument.ToString();

            TableDataInit();

            DDL_Class.Enabled = false;
        }
        catch (Exception ex)
        {
            LB_ManageNewsClass.Text = ex.Message;
        }
    }
    #endregion
    #region 删除类别 [Command] [GridView]
    protected void LBtn_Del_Command(object sender, CommandEventArgs e)
    {
        try
        {
            MNC.NC_ID = Convert.ToInt32(e.CommandArgument);

            BNC.Del(MNC);

            LB_ManageNewsClass.Text = "删除成功";

            GV_NewsClassList.DataBind();

            DDL_ClassDataBind();
        }
        catch (Exception ex)
        {
            LB_ManageNewsClass.Text = ex.Message;
        }
    }
    #endregion


    #region 添加类别 [Click]
    protected void Btn_Add_Click(object sender, EventArgs e)
    {
        try
        {
            MNC.NC_ID = Convert.ToInt32(DDL_Class.SelectedValue);
            MNC.NC_Title = TB_Title.Text;
            MNC.NC_IsLock = false;

            BNC.Add(MNC);

            LB_ManageNewsClass.Text = "添加成功";

            GV_NewsClassList.DataBind();

            DDL_ClassDataBind();
        }
        catch (Exception ex)
        {
            LB_ManageNewsClass.Text = ex.Message;
        }
    }
    #endregion


    #region 修改类别 [Click]
    protected void Btn_Mod_Click(object sender, EventArgs e)
    {
        try
        {
            MNC.NC_Title = TB_Title.Text;
            MNC.NC_ID = Convert.ToInt32(LB_ID.Text);

            BNC.Mod(MNC);

            LB_ManageNewsClass.Text = "修改成功";

            GV_NewsClassList.DataBind();

            DDL_ClassDataBind();
        }
        catch (Exception ex)
        {
            LB_ManageNewsClass.Text = ex.Message;
        }
    }
    #endregion
}
